package io.imp.modules.tasks.unionqrc.task;

import io.imp.common.utils.DateUtils;
import io.imp.modules.job.task.ITask;
import io.imp.modules.tasks.unionqrc.entity.*;
import io.imp.modules.tasks.unionqrc.service.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 功能描述:
 * <按时间范围生成数据文件>
 *
 * @ClassName: ItcUnionqrcPackFile
 * @Date: 2019年05月17日 13:55:46
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
@Component("itcUnionqrcPackFile")
public class ItcUnionqrcPackFile extends BaseService implements ITask {
    public static final String ItcsUnionpayTxnJnls = "渠道订单";
    public static final String ItcsUnionpayCancelJnls = "渠道撤销";
    public static final String ItcsUnionpayRefundJnls = "渠道退款";
    public static final String ItcsUnionpayReverseJnls = "渠道冲正";


    private Logger logger = LoggerFactory.getLogger(getClass());

    LinkedList<ItcsUnionpayTxnJnl> itcs_unionpay_txn = new LinkedList<ItcsUnionpayTxnJnl>();

    /**
     * 银联上送文件方法
     */
    @Override
    public void run(String params){
        logger.info("进入银联上送文件方法");
        long startTime=System.currentTimeMillis();
        //站点结果集合
        Map<String,String> stationMap = getItcsStations();
        //String结果集合
        LinkedList<String> resultList = new LinkedList<String>();

        //渠道用时间
        Date date = new  Date();
        String rDateStr = DateUtils.getDateStr(date,"yyyyMMddHHmm");
        logger.info("当前时间为：{}",rDateStr);

        //获取当天0点时间
        Date dateStart = DateUtils.parseDate(DateUtils.getDateStr(date,"yyyyMMdd0000"),"yyyyMMddHHmm");
        //获取下一天0点时间
        Date nextDateStart = DateUtils.parseDate(DateUtils.getDateStr(DateUtils.add(date,Calendar.DAY_OF_MONTH,1),
                "yyyyMMdd0000"),"yyyyMMddHHmm");

        //数据范围时间间隔取自参数表
        String timeInterval = sysDictService.getDictValue("unionqrc","timeInterval");
        int interval = StringUtils.isBlank(timeInterval)? 10 : (Integer.valueOf(timeInterval));
        //为了防止漏数据，左边界在扩大2分钟
        Date lDate = DateUtils.add(date,Calendar.MINUTE,-(interval+2));
        //获取当前日期
        Date createtime = DateUtils.parseDate(DateUtils.getDateStr(lDate,"yyyy-MM-dd"),"yyyy-MM-dd");
        String lDateStr = DateUtils.getDateStr(lDate,"yyyyMMddHHmm");
        if(DateUtils.isSameDay(date,lDate)){
            if((nextDateStart.getTime()-date.getTime()) <= interval*60*1000){
                rDateStr = DateUtils.getDateStr(nextDateStart,"yyyyMMddHHmm");
            }
        }else{
            lDateStr =  DateUtils.getDateStr(dateStart,"yyyyMMddHHmm");
        }
        logger.info("获取{}天 {} - {} 的订单流水",createtime, lDateStr, rDateStr);

        HashMap<String,List> hashMap = new HashMap<>();
        //获取时间段内渠道实体属性集合
        List<ItcsUnionpayTxnJnl> itcsUnionpayTxnJnls = itcsUnionpayTxnJnlService.findItcsUnionpayTxnJnls2(rDateStr,lDateStr);
        //获取时间段内撤销集合
        List<ItcsUnionpayCancelJnl> itcsUnionpayCancelJnls = itcsUnionpayCancelJnlService.getItcsUnionpayCancelJnlsByTime(rDateStr,lDateStr);
        //获取时间段内退款集合
        List<ItcsUnionpayRefundJnl> itcsUnionpayRefundJnls = itcsUnionpayRefundJnlService.getItcsUnionpayRefundJnlsByTime(rDateStr,lDateStr);
        //获取时间段内冲正集合
        List<ItcsUnionpayReverseJnl> itcsUnionpayReverseJnls = itcsUnionpayReverseJnlService.getItcsUnionpayReverseJnlsByTime(rDateStr,lDateStr);

        logger.info("时间段内渠道实体集合，共{}条",itcsUnionpayTxnJnls.size());
        logger.info("时间段内撤销实体集合，共{}条",itcsUnionpayCancelJnls.size());
        logger.info("时间段内退款实体集合，共{}条",itcsUnionpayRefundJnls.size());
        logger.info("时间段内冲正实体集合，共{}条",itcsUnionpayReverseJnls.size());

        hashMap.put(ItcsUnionpayTxnJnls,itcsUnionpayTxnJnls);
        hashMap.put(ItcsUnionpayCancelJnls,itcsUnionpayCancelJnls);
        hashMap.put(ItcsUnionpayRefundJnls,itcsUnionpayRefundJnls);
        hashMap.put(ItcsUnionpayReverseJnls,itcsUnionpayReverseJnls);
        //获取时间段内流水集合
        Date nowTime2 = DateUtils.parseDate(DateUtils.add(rDateStr,"yyyyMMddHHmm",Calendar.MINUTE,5),"yyyyMMddHHmm");;
        Date lTime2 = DateUtils.parseDate(DateUtils.add(lDateStr,"yyyyMMddHHmm",Calendar.MINUTE,-5),"yyyyMMddHHmm");
        logger.info("流水订单表的当前时间为：{}",nowTime2);
        logger.info("流水订单表的过去时间为：{}",lTime2);
        Map<String,ItcsTransOrderJnl> itcstransOrderJnlMap = getItcstransOrderJnls(createtime,nowTime2,lTime2);
        logger.info("时间段内流水实体集合，共{}条",itcstransOrderJnlMap.size());

        long mTime=System.currentTimeMillis();
        float rmTime=(float)(mTime-startTime)/1000;
        System.out.println("执行时间："+rmTime+"s");

        String result = getStringSource(hashMap,itcstransOrderJnlMap,stationMap);
        System.out.println(result);
        String path = sysDictService.getDictValue("unionqrc","filePath");
        String prefix = "GS";
        String suffix = ".txt";
        if (StringUtils.isBlank(result)){
            logger.info("没有需要打印的数据");
            return;
        }
        resultList.add(result);
        String today = DateUtils.getDateStr(date,"yyyyMMdd");
        writeFile(path,today,prefix,suffix,resultList);

        long endTime=System.currentTimeMillis();
        float excTime=(float)(endTime-startTime)/1000;
        logger.info("执行时间："+excTime+"s");
        logger.info("打印完毕");
    }

}
